package net.narusas.aceauction.push.messageapp;

import javax.swing.JTextArea;

import net.narusas.aceauction.push.SchedulePushService;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public class LogAppender extends AppenderSkeleton {

	private static JTextArea logTextArea;

	public LogAppender() {

	}

	public LogAppender(JTextArea logTextArea) {
		LogAppender.logTextArea = logTextArea;
	}

	@Override
	public void close() {
	}

	@Override
	public boolean requiresLayout() {
		return false;
	}

	@Override
	protected synchronized void append(LoggingEvent event) {
		if (LogAppender.logTextArea == null) {
			return;
		}
		if (event.getLoggerName() == null || event.getLoggerName().startsWith("net.narusas") == false) {
			return;
		}
		System.out.println(event.getLoggerName());
		String message = event.getMessage().toString();
		String txt = logTextArea.getText();
		txt = txt + "\n" + message;
		if (txt.length() > 10000) {
			txt = txt.substring(txt.length() - 10000, txt.length() - 1);
		}
		logTextArea.setText(txt);
		int index = txt.lastIndexOf('\n');
		if (index != -1) {
			logTextArea.setCaretPosition(index + 1);
		}
	}

}
